<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/21
 * Time: 下午9:22
 */

namespace common\server;

use common\logic\BuildingTypeLogic;
use common\logic\DecorationTypeLogic;
use common\logic\HouseCategoryLogic;
use common\logic\HouseMarkLogic;
use common\logic\HouseTypeLogic;
use common\logic\RegionLogic;
use common\logic\UnitPriceLogic;
use common\models\BaseRegionModel;
use common\models\BaseHouseMarkModel;
use common\logic\RentingPriceLogic;
use common\logic\MenuLogic;
use common\logic\RoleLogic;
use common\logic\AdvertLogic;
use common\logic\AdverttypeLogic;
use common\logic\RentingBrandLogic;


use yii\helpers\ArrayHelper;


/**
 * Class BaseDataServer
 * 基础数据
 *
 * @package common\server
 */
class BasicDataServer extends Server
{

    const Y_N = [
        0 => '否',
        1 => '是'
    ];

    const I_R = [
        0 => '不推荐',
        1 => '是推荐'
    ];

    /**
     * @var array 状态
     */
    const COMMON_STATUS = ['1' => '启用', '0' => '禁用'];

    /**
     * @var array 是否显示
     */
    const COMMON_SHOW = ['1' => '显示', '0' => '隐藏'];


    /**
     * @param string $key
     * @return string
     */
    public function getShow($key)
    {
        return self::COMMON_SHOW[$key];
    }

    /**
     * 菜单状态
     *
     * @param string $key
     * @return string
     */
    public function getStatus($key)
    {
        return self::COMMON_STATUS[$key];
    }


    public function getYN()
    {
        return self::Y_N;
    }

    public function getIsRecommend()
    {
        return self::I_R;
    }


    /**
     * 是否
     * @param $val
     *
     * @return mixed|string
     */
    public function getYNText($val){
        return empty(self::Y_N[$val]) ? '' : self::Y_N[$val];
    }

    /**
     * 接口用基础数据
     * @return array
     */
    public function getSearchData()
    {
        $regions = RegionLogic::instance()->getFormatChild();

        return ['regions' => $regions,
            'prices' => $this->getUitPricesToSearch(),
            'houseType' => HouseTypeLogic::instance()->getAll(),
            'category' => HouseCategoryLogic::instance()->getAll()
        ];
    }


    /**
     * 租房接口用基础数据
     * @return array
     */
    public function getRentingSearchData()
    {
        $regions = RegionLogic::instance()->getFormatChild();

        return ['regions' => $regions,
            'prices' => $this->getRentingPricesToSearch(),
        ];
    }

   /**
    *pc 接口基础数据
    */
    public function getPcSearchData(){
        $regions = RegionLogic::instance()->getFormatChild();

        return ['regions' => $regions,
            'rentprices' => $this->getRentingPricesToSearch(),
            'prices' => $this->getUitPricesToSearch(),
            'houseType' => HouseTypeLogic::instance()->getAll(),
            'category' => HouseCategoryLogic::instance()->getAll()
        ];
    }

    /**
     * 租房搜索单价区间
     * @return array
     */
    public function getRentingPricesToSearch()
    {
        $prices = RentingPriceLogic::instance()->getAll();
        $data = [];
        foreach ($prices as $price) {
            if (empty($price->min_price)) {
                $text = $price->max_price . '元/月以下';
            } else
                if (empty($price->max_price)) {
                    $text = $price->min_price . '元/月以上';
                } else {
                    $text = $price->min_price . '-' . $price->max_price.'元/月';
                }
            $data[] = ['id' => $price->id, 'text' => $text];
        }

        return $data;
    }

    /**
     * 搜索单价区间
     * @return array
     */
    public function getUitPricesToSearch()
    {
        $prices = UnitPriceLogic::instance()->getAll();
        $data = [];
        foreach ($prices as $price) {
            if (empty($price->min_price)) {
                $text = $price->max_price . '元/㎡以下';
            } else
                if (empty($price->max_price)) {
                    $text = $price->min_price . '元/㎡以上';
                } else {
                    $text = $price->min_price . '-' . $price->max_price.'元/㎡';
                }
            $data[] = ['id' => $price->id, 'text' => $text];
        }

        return $data;
    }

    /**
     * 楼盘类别
     *
     * @return array
     */
    public function getCategoryToOptions()
    {
        $options = HouseCategoryLogic::instance()->getAll();

        return ArrayHelper::map($options, 'id', 'category');
    }

    /**
     * 装修类型
     *
     * @return array
     */
    public function getDecorationTypeToOptions()
    {
        $options = DecorationTypeLogic::instance()->getAll();

        return ArrayHelper::map($options, 'id', 'decoration_type');
    }

    /**
     * 地区
     * @return array
     */
    public function getRegionToOptions($search = false)
    {
        $regions = RegionLogic::instance()->getAll();
        $dataList = [];
        if($search){
            $dataList [0] = '请选择地区';
        }
        $this->_formatRegionToOption($regions, 0, $dataList);
        return $dataList;
    }


    /**
     * @param $parentId
     *
     * @return array
     */
    public function getChildRegion($parentId){
        $regions = RegionLogic::instance()->getChildCity($parentId);
        return ArrayHelper::getColumn($regions,'id');
    }

	public function getChildRegionByParent($pid=0){
        $options = RegionLogic::instance()->getChildCity($pid);
        $dataList = [];
        foreach ($options as $k => $region) {
             $dataList[$region->id] = $region->region_name;
        }
        return $dataList;
    }
    /**
     * @param BaseRegionModel[] $regions
     * @param integer           $parent_id
     * @param array             $options
     * @param int               $index
     */
    protected function _formatRegionToOption($regions, $parent_id, array &$options, $index = 0)
    {
        $left = '';
        if ($index > 0) {
            $left = join('', array_fill(0, $index * 4, ' '));
        }
        if (!empty($regions)) {
            foreach ($regions as $k => $region) {
                if ($region->parent == $parent_id) {
                    $options[$region->id] = $left . $region->region_name;
                    unset($regions[$k]);
                    $this->_formatRegionToOption($regions, $region->id, $options, $index + 1);
                }
            }
        }
    }

    /**
     * 建筑类型
     * @return array
     */
    public function getBuildTypeToOptions(){
        $options = BuildingTypeLogic::instance()->getAll();
        return ArrayHelper::map($options,'text','text');
    }

    /**
     * 楼盘标签
     * @return array
     */
    public function getHouseMarkToOptions($type=1){
        $options = HouseMarkLogic::instance()->getAll($type);
        return ArrayHelper::map($options,'id','mark');
    }
    
    public function getMenuRuleToOptions(){
        $options = MenuLogic::instance()->getAll();
        return ArrayHelper::map($options,'id','title');
    }
    
    public function getRoleToOptions(){
        $options = RoleLogic::instance()->getAllRule();
        return ArrayHelper::map($options,'id','name');
    }
	
	
	public function getAdvTypeOptions(){
        $options = AdverttypeLogic::instance()->getAll();
        return ArrayHelper::map($options,'tid','typename');
    }
	
	public function getBrandidOptions(){
        $options = RentingBrandLogic::instance()->getAll();
        return ArrayHelper::map($options,'id','bname');
    }
	
	public function getHouseSize(){
		
	}
    
}